//
//  BLLiveHeaderView.m
//  blanket
//
//  Created by Mac on 2018/11/1.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BLLiveHeaderView.h"
#import "STPageView.h"
@interface BLLiveHeaderView()
@property(nonatomic, strong) STPageView                     *pagetView;/**<  */
@end
@implementation BLLiveHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubView];
    }
    return self;
}
- (void)resetSubView{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    [self configSubView];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.pagetView = [[STPageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 230)];
    self.pagetView.autoMoveDuring = 4;
    self.pagetView.imageArray = @[[UIImage imageNamed:@"轮播图"],[UIImage imageNamed:@"轮播图"],[UIImage imageNamed:@"轮播图"]];
    self.pagetView.imageUrlArray = self.bannerArray;
    [self.pagetView setOnSelectedBannerHandle:^(NSInteger index) {
//        if (weakSelf.onSlectedAdverView) {
//            GHAdverModel * model = weakSelf.adverArray[index];
//            weakSelf.onSlectedAdverView(model);
//        }
    }];
    [self addSubview:self.pagetView];
    self.height = self.pagetView.bottom;
    NSArray * array = @[@{@"imageName":@"所有产品",@"title":@"所有"},
                        @{@"imageName":@"24布料床单",@"title":@"床单"},
                        @{@"imageName":@"被套",@"title":@"被套"},
                        @{@"imageName":@"浴巾",@"title":@"浴巾"},
                        @{@"imageName":@"毛巾",@"title":@"面巾"},
                        @{@"imageName":@"浴袍",@"title":@"浴袍"},
                        @{@"imageName":@"洗涑",@"title":@"洗涑"},
                        @{@"imageName":@"地巾",@"title":@"地巾"}];
    array = self.cidArray;
    CGFloat menuWith = UIScreenWidth / 4;
    CGFloat menuHeight = 90;
    for (NSInteger i = 0; i < array.count; i ++) {
        NSDictionary * dic = array[i];
        NSString * imageName = dic[@"img"];
        NSString *  title = dic[@"cate_name"];
        NSInteger  cid = [dic[@"cid"] integerValue];
//        "cid": "1",
//        "img": "http:\/\/192.168.0.228\/Api\/Hotel\/cate.html\/Uploads\/20181022\/5bcd44d305aa0.jpg",
//        "cate_name": "\u68b3\u5b501"
        STMenuControl * menu = [[STMenuControl alloc] initWithFrame:CGRectMake(0, self.pagetView.bottom , menuWith, menuHeight) imageName:imageName title:title titleColor:FirstTextColor];
        menu.left = (i%4) * menuWith;
         menu.top = (i/4) * menuHeight + self.pagetView.bottom ;
        [menu.imageButton sd_setImageWithURL:[NSURL URLWithString:imageName] forState:UIControlStateNormal];
        menu.tag = cid  + 10000;
        [menu addTarget:self action:@selector(onSelctedControl:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:menu];

        if (i < 4) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 1)];
            imageView.image = [UIImage imageNamed:@"形状1拷贝2"];
            [self addSubview:imageView];
            imageView.centerX = menu.centerX;
            imageView.bottom = menu.bottom - 2;
            [self addSubview:imageView];
        }
        if (i % 4  < 3) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1, 54)];
            imageView.image = [UIImage imageNamed:@"形状1拷贝2"];
            [self addSubview:imageView];
            imageView.centerY = menu.centerY;
            imageView.right = menu.right - 2;
            [self addSubview:imageView];
        }
        self.height = menu.bottom + 20;
    }
//    NSArray * imagearray = self.bannerArray;
//    for (NSString * url in imagearray) {
//        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height, UIScreenWidth, 140)];
//        [imageView sd_setImageWithURL:[NSURL URLWithString:url]];
//        [self addSubview:imageView];
//        self.height = imageView.bottom + 10;
//    }
}
- (void)onSelctedControl:(STMenuControl*)menuControl{
    NSString * tag = @(menuControl.tag - 10000).description;
    if (self.onSlectedControl) {
        self.onSlectedControl(tag,menuControl.titleLable.text);
    }
}
@end
